﻿using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using HandyControl.Controls;
using HardToolApp.Model;
using HardToolApp.Tool;
using HardToolApp.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace HardToolApp.ViewModel
{
    [ViewModelHelper("设置", typeof(SetPage), typeof(UpdateModel), Sort = 10)]
    internal class SetVM: PeripheralVM
    {
        public UpdateModel update { get; set; } = new UpdateModel();
        public RelayCommand GetVersionCmd { get; set; }
        public RelayCommand UpdateCmd { get; set; }

        public SetVM() 
        {
            GetVersionCmd = new RelayCommand(GetVersion);
            UpdateCmd = new RelayCommand(StartUpdate);
        }

        private void GetVersion()
        {
            update.subCmd = (byte)UpdateModel.SubCmd.Version;
            Send(update);
        }

        private void StartUpdate()
        {
            update.subCmd = (byte)UpdateModel.SubCmd.Begin;
            Send(update);
        }

        public override void Rec(object t, byte[] b)
        {
            switch (b[4])
            {
                case (byte)CommSubUpdate.COMM_SUB_UPDATE_VERSION:
                    update.Version = UpdateModel.VerStr(b[6], b[7], b[8], b[9]);
                    break;
                case (byte)CommSubUpdate.COMM_SUB_UPDATE_BEGIN:
                    if (b[5] == 1)
                    {
                        Growl.Warning("通知在线升级失败");
                    }
                    break;
                case (byte)CommSubUpdate.COMM_SUB_UPDATE_DATA_SEND:
                    update.subCmd = (byte)UpdateModel.SubCmd.Send;
                    update.pkgNow = (UInt16)((b[5] << 8) + b[6]);
                    Send(update);
                    break;
                case (byte)CommSubUpdate.COMM_SUB_UPDATE_STOP:
                    switch(b[5])
                    {
                        case 0:
                            Growl.Success("在线升级成功");
                            break;
                        case 1:
                            Growl.Warning("固件写入失败");
                            break;
                        case 2:
                            Growl.Warning("固件长度超出");
                            break;
                    }
                    update.subCmd = (byte)UpdateModel.SubCmd.Stop;
                    Send(update);
                    break;

            }
        }
        #region 在线升级
        public override void Send(Peripheral peripheral)
        {
            UpdateModel m = (UpdateModel)peripheral;
            if (m.subCmd == (byte)UpdateModel.SubCmd.Version)
            {
                byte[] bytes = new byte[6];
                bytes[0] = 0x55;
                bytes[1] = 0xaa;
                bytes[2] = (byte)bytes.Length;
                bytes[3] = (byte)CommCmd.COMM_UPDATE;
                bytes[4] = (byte)CommSubUpdate.COMM_SUB_UPDATE_VERSION;
                bytes[5] = Common.chksum(bytes);
                USBInfoVM.USBVm.send(bytes);
            }
            else if (m.subCmd == (byte)UpdateModel.SubCmd.Begin)
            {
                byte[] bytes = new byte[6];
                bytes[0] = 0x55;
                bytes[1] = 0xaa;
                bytes[2] = (byte)bytes.Length;
                bytes[3] = (byte)CommCmd.COMM_UPDATE;
                bytes[4] = (byte)CommSubUpdate.COMM_SUB_UPDATE_BEGIN;
                bytes[5] = Common.chksum(bytes);
                USBInfoVM.USBVm.send(bytes);
            }
            else if (m.subCmd == (byte)UpdateModel.SubCmd.Send)
            {
                byte[] sendbytes = m.GetBytes();
                byte[] bytes = new byte[sendbytes.Length + 12];
                bytes[0] = 0xaa;
                bytes[1] = 0x55;
                bytes[2] = (byte)bytes.Length;
                bytes[3] = (byte)CommCmd.COMM_UPDATE;
                bytes[4] = (byte)CommSubUpdate.COMM_SUB_UPDATE_DATA_SEND;
                bytes[5] = (byte)(m.pkgAll >> 8 & 0xff);
                bytes[6] = (byte)(m.pkgAll & 0xff);
                bytes[7] = (byte)(m.pkgNow >> 8 & 0xff);
                bytes[8] = (byte)(m.pkgNow & 0xff);
                bytes[9] = (byte)(sendbytes.Length >> 8 & 0xff);
                bytes[10] = (byte)(sendbytes.Length & 0xff);
                sendbytes.CopyTo(bytes, 11);
                bytes[11 + sendbytes.Length] = Common.chksum(bytes);
                USBInfoVM.USBVm.send(bytes);
            }
            else if (m.subCmd == (byte)UpdateModel.SubCmd.Stop)
            {
                byte[] bytes = new byte[7];
                bytes[0] = 0xaa;
                bytes[1] = 0x55;
                bytes[2] = (byte)bytes.Length;
                bytes[3] = (byte)CommCmd.COMM_UPDATE;
                bytes[4] = (byte)CommSubUpdate.COMM_SUB_UPDATE_STOP;
                bytes[5] = (byte)0;
                bytes[6] = Common.chksum(bytes);
                USBInfoVM.USBVm.send(bytes);
            }

        }
        #endregion
    }
}
